import { $event } from '/utils'
import { GlobalData } from "/store/state";

interface ItemOption {
  // 选项文字
  title: string
  // 跳转地址。为空则点击无反应
  url?: string
  // 点击时执行的处理函数
  method?: Function
  // 描述文字，可使用\n转义符进行换行
  desc?: string
  // 是否开启当前选项与下一个选项的间距
  enableBottom?: boolean
  // 选项文字是否加粗
  fontWeight?: boolean
}

Component({
  /**
   * Component properties
   */
  properties: {},

  /**
   * Component initial data [private]
   */
  data: {
    show: false,
    itemList: [] as Array<ItemOption>,
    isFullScreen: GlobalData.isFullScreen,
    navigationHeight: GlobalData.navigationHeight,
  },

  /**
   * lifecycle of component
   */
  pageLifetimes: {
    show() {
      $event.on('after:boot', this, () => {
        this.getPostition()
      })
      $event.on('showSheet', this, async (itemList: Array<ItemOption>) => {
        this.setData({ itemList })
        this.onShow()
      })
      this.getPostition()
    },
  },
  /**
   * Component methods
   */
  methods: {
    getPostition() {
      this.setData({
        isFullScreen: GlobalData.isFullScreen,
        navigationHeight: GlobalData.navigationHeight,
      })
    },
    onShow() {
      this.setData({ show: true })
    },
    onHide() {
      this.setData({ show: false })
    },
    async toDetail(e: any) {
      const index = +e.currentTarget.dataset.index
      const item = this.data.itemList[index]
      if (item.method) {
        await item.method()
      }
      if (item.url) {
        wx.navigateTo({ url: item.url })
      }
      this.onHide()
    },
  },
})
